package com.example.rg1broadcastreceiverdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private Button buttonSend;
    private Button buttonReg;
    private MyReceiver myReceiver;
    private NetworkStateReceiver networkStateReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        buttonSend=findViewById(R.id.buttonSend);
        buttonSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.setAction("com.example.broadcast.MY_BROADCAST");
                intent.putExtra("msg","All I can do is beginning!");
                sendOrderedBroadcast(intent,null);
            }
        });
        buttonReg=findViewById(R.id.buttonReg);
        buttonReg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                IntentFilter intentFilter=new IntentFilter();
                intentFilter.addAction("com.example.broadcast.MY_BROADCAST");
                intentFilter.setPriority(10);
                myReceiver=new MyReceiver();
                registerReceiver(myReceiver,intentFilter);
            }
        });
        networkStateReceiver=new NetworkStateReceiver();
        IntentFilter intentFilter=new IntentFilter();
        intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        registerReceiver(networkStateReceiver,intentFilter);

    }
}
